package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.mapper.RoleMapper;
import com.jt.pojo.Rights;
import com.jt.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

/**
 * @author 刘昱江
 * 时间 2021/3/3
 */
@Service
public class RoleServiceImpl implements RoleService{

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RightsMapper rightsMapper;
    @Autowired
    private RightsService rightsService;

    @Override
    public List<Role> findRoleRightsList() {
        //1.查询所有的role角色信息
        List<Role> roleList = roleMapper.selectList(null);
        //2.遍历获取关联的角色信息
        for (Role role : roleList){
            int roleId = role.getRoleId();
            //获取所有的关联权限Id
            List<Integer> rightsIds = roleMapper.findRightsIdList(roleId);
            if(rightsIds == null || rightsIds.size()==0) break;

            //将rightsId封装为3级权限菜单
            List<Rights> rightsList = rightsService.getRightsListByIds(rightsIds);
            role.setRightsList(rightsList);
    }
        return roleList;
    }

    @Override
    public Role findRoleRightsListByRoleId(Integer roleId) {
        Role role = roleMapper.selectById(roleId);
        //获取所有的关联权限Id
        List<Integer> rightsIds = roleMapper.findRightsIdList(roleId);
        if(rightsIds == null || rightsIds.size()==0) {
            return role;
        }
        //将rightsId封装为3级权限菜单
        List<Rights> rightsList = rightsService.getRightsListByIds(rightsIds);
        role.setRightsList(rightsList);
        return role;
    }

    @Override
    public Role deleteRoleRights(Integer roleId, Integer rightsId, Integer level) {

        //1.根据不同类型删除数据
        if(level == 3){
            //三级菜单没有子级,删除中间表
            roleMapper.deleteRoleRightsById(roleId,rightsId);
        }
        if(level == 2){
            List<Integer> rightsIds = rightsService.getRightsChildrenList(rightsId);
            rightsIds.add(rightsId);
            roleMapper.deleteRoleRights(roleId,rightsIds);
        }

        if(level == 1){
            List<Integer> rightsIds = new ArrayList<>();
            List<Integer> twoIdList = rightsService.getRightsChildrenList(rightsId);
            for(Integer twoRightsId : twoIdList){
                List<Integer> threeIdList = rightsService.getRightsChildrenList(twoRightsId);
                for(Integer threeId : threeIdList){
                    rightsIds.add(threeId);
                }
                rightsIds.add(twoRightsId);
            }
            rightsIds.add(rightsId);
            roleMapper.deleteRoleRights(roleId,rightsIds);
        }
        return findRoleRightsListByRoleId(roleId);
    }

    @Override
    public List<Integer> findRightsIdByOrderId(Integer roleId) {

        return roleMapper.findRightsIdList(roleId);
    }

    @Override
    @Transactional
    public void saveRights(Integer roleId, Integer[] rightsIds) {
        //保存角色对应的权限时,应该先删除原有数据
        roleMapper.deleteRightsAllByRoleId(roleId);
        //删除之后新增角色和权限的数据
        roleMapper.insertRightsByRoleId(roleId,rightsIds);

    }

    @Override
    public List<Role> findRoleList() {

        return roleMapper.selectList(null);
    }

}
